import java.util.Objects;
import java.util.Scanner;
import java.sql.*;

public class sign_in {
    public static boolean name_pp(String name) throws SQLException {

        Connection conn=JDBC.getConnection();

        String sql="SELECT name FROM User WHERE name='"+name+"'";
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        ResultSet set=null;
        if (ps != null) {
            set=ps.executeQuery();
        }
        return set != null && set.next();
    }
    public static boolean newuser(String name,String word) throws SQLException {
        String sql="INSERT INTO User(name,password) VALUES ('"+name+"','"+word+"')";
        int rows=JDBC.executeUpdate(sql);
        return rows>0;
    }
    public static void zhuce() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("*******************************");
        System.out.println("***********注册管理账户**********");
        System.out.println("*******************************");
        System.out.println("请输入注册用户名");
        String name=sc.next();
        if(name_pp(name)){
            System.out.println("用户名重复");
        }
        System.out.println("请输入登录密码");
        String userpassword=sc.next();
        System.out.println("请再次输入登录密码");
        String userpassword_pp=sc.next();
        if(!Objects.equals(userpassword_pp, userpassword)){
            System.out.println("输入的两次密码不同");
        }else{
            if(newuser(name,userpassword)){
                System.out.println("成功创建用户");
            }else{
                System.out.println("创建用户异常");
            }
        }
    }
}
